<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_Mailbox extends Controller_Admin_Base
{
    public function action_index()
    {   
        if (HTTP_Request::POST == $this->request->method()) {
            $action = $this->request->post('action');
            $value  = $this->request->post('value');
            $ids    = $this->request->post('ids');
                  
            try {  
                if (!in_array($action, array('assign', 'status', 'delete'))) {
                    throw new Controller_Exception('Некорректное действие');   
                }
                
                if ($action == 'assign') {
                    foreach ($ids as $id) {
                        $ticket = ORM::factory('ticket', $id);
                        $ticket->edit_ticket_assigned($value);   
                    }    
                } else if ($action == 'status') {
                    foreach ($ids as $id) {
                        $ticket = ORM::factory('ticket', $id);
                        $ticket->edit_ticket_status($value);   
                    }    
                } else if ($action == 'delete') {
                    foreach ($ids as $id) {
                        $ticket = ORM::factory('ticket', $id);
                        $ticket->mark_ticket_as_deleted();   
                    } 
                }
            } catch (Model_Exception $e) {
                
            }
        }
         
        $mailbox_hash = $this->request->param('mailbox_hash') ? $this->request->param('mailbox_hash') : null;
        $mailbox      = ORM::factory('mailbox')->get_mailbox_by_hash($mailbox_hash);
        
        $filter       = $this->request->param('filter') ? $this->request->param('filter') : null;
        if (!$filter) {
            header('Location: '.Helper_Admin::mailbox_url($mailbox));
            die();    
        }
        $available_filters = Model_Ticket::get_available_filters();
        $filter  = $available_filters[$filter];
        
        $page = null;
        //$page         = $this->request->param('page') ? $this->request->param('page') : 1;
        
        $content = new View('admin/mailbox/index_tpl');
        $content->mailbox = $mailbox;
        $content->tickets = ORM::factory('ticket')->get_tickets_by_filter($mailbox, $filter, $page);
        $content->filter = $filter; 
        
        $count        = ORM::factory('ticket')->get_tickets_by_filter($mailbox, $filter, $page, true, true);
        $count_active = ORM::factory('ticket')->get_tickets_by_filter($mailbox, $filter, $page, true);
        $content->pagination = array(                                                   
            'page' => $page,
            'per_page' => Model_Ticket::get_tickets_per_page(),
            'count' => $count,
        );
        $content->count_active = $count_active;
        
        $content->all_users = ORM::factory('user')->get_all_users();
        $content->available_statuses = ORM::factory('ticket')->get_available_statuses();
             
        $this->print_template($filter['name'].' - '.'Тикеты', $content);     
    }
    
    public function action_add()
    { 
        if (HTTP_Request::POST == $this->request->method()) {
            $email = $this->request->post('mailbox_email');
            $name  = $this->request->post('name');
                  
            try {        
                $mailbox = ORM::factory('mailbox')->add_mailbox($email, $name);

                Request::current()->redirect(Helper_Admin::forward_mailbox_url($mailbox)); 
            } catch (Model_Exception $e) {
                
            }
        }
       
        $content = new View('admin/mailbox/add_tpl');       
        $this->print_template('Создать почтовый ящик', $content);   
    }
    
    public function action_forward()
    { 
        $mailbox_hash = $this->request->param('mailbox_hash') ? $this->request->param('mailbox_hash') : null;
        $mailbox      = ORM::factory('mailbox')->get_mailbox_by_hash($mailbox_hash);
        
        $content = new View('admin/mailbox/forward_tpl');
        $content->mailbox = $mailbox;       
        $this->print_template('Создать почтовый ящик', $content);   
    }
    
    public function action_delete()
    { 
        $mailbox_hash = $this->request->param('mailbox_hash') ? $this->request->param('mailbox_hash') : null;
        $mailbox      = ORM::factory('mailbox')->get_mailbox_by_hash($mailbox_hash);
        
        $mailbox->mark_mailbox_as_deleted();
        
        Request::current()->redirect('/dashboard');  
    }
    
    public function action_save_reply()
    {
        $mailbox_hash = $this->request->param('mailbox_hash') ? $this->request->param('mailbox_hash') : null;
        $mailbox      = ORM::factory('mailbox')->get_mailbox_by_hash($mailbox_hash);
        
        $result = array(
            'success'  => false,
        );
        if ($this->request->is_ajax()) {
            $name  = trim($this->request->post('name'));
            $reply = trim($this->request->post('reply'));
            if ($name && $reply) {
                $pattern = ORM::factory('pattern')->add_pattern($mailbox, $name, $reply);
                
                $append = '<li>   
    <a href="#" rel="tooltip" onclick="$(\'#editor\').focus(); pasteHtmlAtCaret($(this).parent().find(\'div\').html()); return false;">'.$pattern->name.'</a>
    <div style="display: none;">'.$pattern->reply.'</div>
</li>
<li class="divider"></li>';
                              
                $result = array(
                    'success' => true,
                    'append'  => $append,
                );
            }
        }
        //header('Content-type: text/json');
        echo json_encode($result);
        die();
    }
} 
